package tests.jdl.databaseObjects;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import jdl.databaseObjects.DbField;
import jdl.databaseObjects.DbType;
import jdl.databaseObjects.FieldCollection;
import jdl.databaseObjects.ObjectAlreadyExistsException;
import jdl.databaseObjects.Table;
import jdl.databaseObjects.sqlite.datatypes.IntDbType;
import jdl.databaseObjects.sqlite.datatypes.VarcharDbType;
import junit.framework.Assert;

public class FieldCollection_Test {
	private FieldCollection fieldCollection;

	@Before
	public void setUp() {
		Table table = new Table("hello");
		fieldCollection = new FieldCollection(table);
	}

	@Test
	public void addField_addOneField() {
		DbField field = new DbField("hello", new IntDbType());
		fieldCollection.addField(field);
		Assert.assertEquals(1, fieldCollection.getCount());
		Assert.assertEquals(field, fieldCollection.getObjectByIndex(0));
		Assert.assertEquals(fieldCollection, field.getFieldCollection());
	}

	@Test
	public void addField_addTwoFields() {
		fieldCollection.addField(new DbField("first", new VarcharDbType(10)));
		fieldCollection.addField(new DbField("second", new IntDbType()));
		Assert.assertEquals(2, fieldCollection.getCount());
	}

	@Test
	public void add_oneField() throws ObjectAlreadyExistsException {
		DbType type = Mockito.mock(DbType.class);
		DbField field = new DbField("name", type);
		fieldCollection.add(field);
		Assert.assertEquals(fieldCollection, field.getFieldCollection());
	}

	@Test
	public void getFieldByName_collectionHasOneField()
			throws ObjectAlreadyExistsException {
		DbType type = Mockito.mock(DbType.class);
		DbField field = new DbField("id", type);
		fieldCollection.add(field);
		Assert.assertEquals(field, fieldCollection.getFieldByName("id"));
	}

	@Test
	public void getTable() {
		Table table = new Table("qwerty");
		fieldCollection = new FieldCollection(table);
		Assert.assertEquals(table, fieldCollection.getTable());
	}

	@Test(expected = IllegalArgumentException.class)
	public void constructor_tableIsNull() {
		new FieldCollection(null);
	}
}
